<template>
  <!-- 投诉建议 -->
  <view class="comAndadvice">
    <u-tabs
      :list="list"
      lineColor="#2488F5"
      lineWidth="25"
      :current="activeIndex"
      :inactiveStyle="inactiveStyle"
      :activeStyle="{ color: '#333333' }"
      @click="tabsClick"
    ></u-tabs>

    <view class="wrapper-content">
      <template v-if="activeIndex == 0">
        <scroll-view
          scroll-y="true"
          @scrolltolower="scrollBottom"
          @scroll="scrollHeight"
          :style="[
            {
              height: !loading && !noMore ? '100%' : '100%',
            },
          ]"
          v-if="dataSource.length"
        >
          <view
            class="allList wrapper-item"
            :class="item.status == 2 ? 'itemActive' : 'itemActive2'"
            v-for="(item, i) in dataSource"
            :key="i"
            :data-url="`/comAndadvice/complaintDetail/complaintDetail?id=${item.id}`"
            @click="handleClickRouter"
          >
            <view class="rtEmployee">
              <view class="line-hidden title">
                {{ item.complaintContent }}
              </view>
              <view class="line-hidden footer">
                投诉分类：<text>{{ item.complaintClassification }}</text>
              </view>
              <view class="line-hidden footer">
                投诉时间：<text>{{ item.complaintTime }}</text>
              </view>
            </view>
          </view>
          <view class="pageBottom" v-if="pageBottom">—— 到底了 ——</view>
        </scroll-view>
      </template>

      <template v-if="activeIndex == 1">
        <scroll-view
          scroll-y="true"
          @scrolltolower="scrollBottom"
          @scroll="scrollHeight"
          :style="[
            {
              height: !loading && !noMore ? '100%' : '100%',
            },
          ]"
          v-if="dataSource.length"
        >
          <view
            class="allList wrapper-item"
            :class="item.status == 2 ? 'itemActive' : 'itemActive2'"
            v-for="(item, i) in dataSource"
            :key="i"
            :data-url="`/comAndadvice/complaintDetail/suggestionDetail?id=${item.id}`"
            @click="handleClickRouter"
          >
            <view class="rtEmployee">
              <view class="line-hidden title">
                {{ item.adviseContent }}
              </view>
              <view class="line-hidden footer">
                建议时间：<text>{{ item.adviseTime }}</text>
              </view>
            </view>
          </view>
          <view class="pageBottom" v-if="pageBottom">—— 到底了 ——</view>
        </scroll-view>
      </template>

      <view v-if="!dataSource.length">
        <u-empty
          mode="list"
          text="暂无内容！"
          :icon="baseImgUrl + 'no-icon2.png'"
        >
        </u-empty>
      </view>
    </view>

    <view class="btns">
      <button
        class="submit-btn"
        type="primary"
        style="width: 100%"
        @click="handleBtn"
      >
        {{ activeIndex == 0 ? "新增投诉" : "新增建议" }}
      </button>
    </view>
  </view>
</template>

<script>
import { complaintMessagePage, adviseMessagePage } from "@/api/home.js";
import { handleClickRouter } from "@/utils/util.js";

export default {
  data() {
    return {
      activeIndex: 0,
      baseImgUrl: this.$baseImgUrl,
      imgPrefix: uni.getStorageSync("imgPrefix"),
      allCode: uni.getStorageSync("allCode"),
      scrollTop: 0,
      list: [
        {
          name: "投诉",
          badge: {
            isDot: false,
          },
        },
        {
          name: "建议",
          badge: {
            isDot: false,
          },
        },
      ],
      dataSource: [],
      pageBottom: false,
      pageConfig: {
        pageNo: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  onLoad() {
    this.scrollTop = 0;
    uni.removeStorageSync("scrollTop");

    this.activeIndex == 0;
    this.pageConfig.pageNo = 1;
    this.dataSource = [];
    this.getList();
  },
  onShow() {
    this.scrollTop = uni.getStorageSync("scrollTop") || 0;

    let comAndadvice = uni.getStorageSync("comAndadvice");
    if (comAndadvice) {
      this.activeIndex == 0;
      this.pageConfig.pageNo = 1;
      this.dataSource = [];
      this.getList();
      uni.setStorageSync("comAndadvice", false);
    }

    let comAndadviceTwo = uni.getStorageSync("comAndadviceTwo");
    if (comAndadviceTwo) {
      this.activeIndex == 1;
      this.pageConfig.pageNo = 1;
      this.dataSource = [];
      this.getList();
      uni.setStorageSync("comAndadviceTwo", false);
    }
  },
  methods: {
    handleClickRouter,
    scrollHeight(e) {
      uni.setStorageSync("scrollTop", e.detail.scrollTop);
    },
    scrollBottom(e) {
      let current = (this.pageConfig.pageNo += 1);
      let pages = Math.ceil(this.pageConfig.total / this.pageConfig.pageSize);
      if (current <= pages) {
        this.getList();
      } else {
        this.pageBottom = true;
      }
    },
    tabsClick(item) {
      this.scrollTop = 0;
      uni.removeStorageSync("scrollTop");

      this.activeIndex = item.index;
      this.pageConfig.pageNo = 1;
      this.dataSource = [];
      this.getList();
    },
    handleBtn() {
      if (this.activeIndex == 0) {
        uni.navigateTo({
          url: `/comAndadvice/newSuggestion/newComplaint`,
        });
      }

      if (this.activeIndex == 1) {
        uni.navigateTo({
          url: `/comAndadvice/newSuggestion/newSuggestion`,
        });
      }
    },
    getList() {
      let data = {
        pageNo: this.pageConfig.pageNo,
        pageSize: this.pageConfig.pageSize,
      };

      if (this.activeIndex == 0) {
        complaintMessagePage(data).then((res) => {
          this.dataSource = this.dataSource.concat(res.result.records || []);
          this.pageConfig.total = res.result.total;

          if (this.dataSource.length) {
            for (var roomv of this.dataSource) {
              this.allCode["complaint_classification"].map((v) => {
                if (v.value == roomv.complaintClassification) {
                  roomv.complaintClassification = v.text;
                }
              });
            }
          }
        });
      }

      if (this.activeIndex == 1) {
        adviseMessagePage(data).then((res) => {
          this.dataSource = this.dataSource.concat(res.result.records || []);
          this.pageConfig.total = res.result.total;
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.comAndadvice {
  height: 100%;

  .wrapper-content {
    padding: 24rpx 16rpx;
    background: #f8f9fb;
    height: calc(100vh - 114px);

    .wrapper-item {
      padding: 30rpx 35rpx;
      margin-bottom: 20rpx;
      border-bottom: none;
      background-color: #ffffff;

      .rtEmployee {
        line-height: 53rpx;
        width: 100%;

        .title {
          font-weight: bold;
          font-size: 32rpx;
          color: #333333;
        }

        .footer {
          color: #a1a1a1;

          text {
            color: #666666;
          }
        }
      }
    }

    .itemActive {
      background: url($baseImgUrl + "advice_icon1.png") no-repeat;
      background-size: 99rpx 85rpx;
      background-position: top right;
      background-color: #ffffff;
    }

    .itemActive2 {
      background: url($baseImgUrl + "advice_icon2.png") no-repeat;
      background-size: 99rpx 85rpx;
      background-position: top right;
      background-color: #ffffff;
    }
  }
}
</style>
